package com.zzz.robot201.service.client;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;

/**
 * 与c语言通信(java做client，c/c++做server，传送一个结构)
 *
 * @author kingfish
 * @version 1.0
 */
public class SendMsg {
	private String serverHost;
	private int serverPort;

	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public int getServerPort() {
		return serverPort;
	}

	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	private byte[] buf = new byte[67]; // 发送数据byte[]

	private int unsigned_int_head[] = new int[]{67,112,21,201};
	private long unsigned_long_long1 = 0;//new Date().getTime();// 当前时间
	private long unsigned_long_long2 = 0;//unsigned_long_long1;// 当前时间
	private float[] float3 = new float[]{0.0f,0.0f,0.0f};// 顺序为x方向速度，y方向速度，转向速度
	private float[] float2 = new float[]{0f,0f}; // 固定 0,0
	// unsigned char 0当前步态序号 4当前状态序号
	private byte[] unsigned_char_byte15= new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

	/**
	 * 构造并转换
	 */
	public SendMsg() {
		checkBuf();
	}

	public SendMsg checkBuf() {
		//byte[] head4 = name.getBytes();
		for (int j=0;j<unsigned_int_head.length;j++){
			byte[] temp = CommonBytes.tolh(unsigned_int_head[j]);
			System.arraycopy(temp, 0, buf, j*4, temp.length);
		}
		byte[] long1 = LongToDword2.longToDword(unsigned_long_long1);
		System.arraycopy(long1, 0, buf, 16, long1.length);

		byte[] long2 = LongToDword2.longToDword(unsigned_long_long2);
		System.arraycopy(long2, 0, buf, 24, long2.length);

		for (int j=0;j<float3.length;j++){
			byte[] temp = CommonBytes.tolh(float3[j]);
			System.arraycopy(temp, 0, buf, 32+j*4, temp.length);
		}
		for (int j=0;j<float2.length;j++){
			byte[] temp = CommonBytes.tolh(float2[j]);
			System.arraycopy(temp, 0, buf, 44+j*4, temp.length);
		}
		System.arraycopy(unsigned_char_byte15, 0, buf, 52, unsigned_char_byte15.length);
		return this;
	}

	public SendMsg(String serverHost, int serverPort, Long date1,Long date2){
		this.serverHost = serverHost;
		this.serverPort = serverPort;
		this.unsigned_long_long1 = date1;
		this.unsigned_long_long2 = date2;
	}

	public SendMsg setUnsigned_int_head(int[] unsigned_int_head) {
		for (int i = 0; i < this.unsigned_int_head.length; i++) {
			if(unsigned_int_head!=null&&unsigned_int_head.length>i){
				this.unsigned_int_head[i] = unsigned_int_head[i];
			}
		}
		return this;
	}

	public SendMsg setFloat3(float[] float3) {
		for (int i = 0; i < this.float3.length; i++) {
			if(float3!=null&&float3.length>i){
				this.float3[i] = float3[i];
			}
		}
		return this;
	}

	public SendMsg setFloat2(float[] float2) {
		for (int i = 0; i < this.float2.length; i++) {
			if(float2!=null&&float2.length>i){
				this.float2[i] = float2[i];
			}
		}
		return this;
	}

	public SendMsg setUnsigned_char_byte15(byte[] unsigned_char_byte15) {
		for (int i = 0; i < this.unsigned_char_byte15.length; i++) {
			if(unsigned_char_byte15!=null&&unsigned_char_byte15.length>i){
				this.unsigned_char_byte15[i] = unsigned_char_byte15[i];
			}
		}
		return this;
	}

	public SendMsg setUnsigned_char_byte15_0(byte byte0) {
		this.unsigned_char_byte15[0] = byte0;
		return this;
	}
	public SendMsg setUnsigned_char_byte15_4(byte byte4) {
		this.unsigned_char_byte15[4] = byte4;
		return this;
	}

	/**
	 * 返回要发送的数组
	 */
	public byte[] getbuf() {
		return buf;
	}

	/**
	 * 发送测试
	 */
	public static void main(String[] args) {
		try {
			int index = 0;
			byte[] receive = new byte[28];

			//String host= "127.0.0.1";
			String host= "192.168.1.223";
            //InetSocketAddress address = new InetSocketAddress(host, 8889);
			Socket socket = new Socket(host, 8889,false);// stream>true:tcp,false:udp
			System.out.println("send content:  "+(1+index));
			InputStream in = socket.getInputStream();
			DataInputStream din = new DataInputStream(in);
            socket.getOutputStream().write(new SendMsg().getbuf());
				/*sock.getInputStream().read(receive);
				SendMsg ee = getEmployee(receive);
				System.out.println("    response:  name="+ee.name+"  "+"value="+ee.id);
				*/
            socket.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
} // end